package light.head.dao.site;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.model.OrganSynopsisAnnex;
import light.head.model.ParkOrgan;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Service;

/**
 * 园区企业
 */
@Service
public class ParkOrganDAO extends BaseDAO<ParkOrgan, Serializable>{
	private static final String sql_indexlist = "SELECT o.name, od.site, o.code FROM ParkOrgan po INNER JOIN  Organ o ON o.code = po.oc LEFT JOIN OrganDetails od  ON od.oc = po.oc WHERE  po.deleted = 0 ORDER BY po.sort desc LIMIT 10  ";
	
	public RESULT findByMap(Map<String, String> map, PageBean pageBean) {
		StringBuffer hql = new StringBuffer("FROM ParkOrgan po WHERE 1 = 1");
		List<Object> list =  new ArrayList<Object>();
		if (map != null) {
			String name=map.get(Parm.NAME);
			String sort = map.get(Parm.SORT);
			String deleted = map.get(Parm.DELETED);
			if(isNotNull(name)){
				hql.append(" AND po.name LIKE ? ");
				list.add("%"+ name +"%");
			}
			if(isNotNull(sort)){
				hql.append(" AND po.sort<= ?");
				list.add(Byte.parseByte(sort));
			}
			if(isNotNull(deleted)){
				hql.append(" AND po.deleted = ?");
				list.add(Boolean.parseBoolean(deleted));
			}
		}
		hql.append(" ORDER BY po.sort ASC");

		return findToPageBean(hql.toString(), pageBean, list);
	}
	//获取首页List
	public List<ParkOrgan> getIndexList(){
		return query(sql_indexlist,null,null);
	}
	//查询是否已添加企业
	public List<ParkOrgan> findOc(Integer code) {
		if (null == code) return null;
		String sql = "FROM ParkOrgan po WHERE po.oc = ?";
		List<ParkOrgan> list = find(sql,code);
		return list;
	}
	//查询序号是否已存在
	public List<ParkOrgan> findSort(Integer sort) {
		String sql = "FROM ParkOrgan po WHERE po.sort = ? AND po.deleted = 0";
		List<ParkOrgan> list = find(sql,sort);
		return list;
	}
	public List<Integer> findOcs() {
		String sql = "select po.oc FROM ParkOrgan po WHERE po.deleted = 0";
		List<Integer> list = find(sql);
		return list;
	}
	/*public List<ParkOrgan> findFileByCodes(List<Integer> codes) {
		if (codes == null || codes.size() ==0) return null;
		String sql = "FROM ParkOrgan po WHERE po.oc IN (";
		List<FileInfo> list = find(sql);
		return list;
	}*/
	
	
	public List<ParkOrgan> findTopNAds(final Integer count) {
		return this.getHibernateTemplate().execute(new HibernateCallback<List<ParkOrgan>>() {
			@SuppressWarnings("unchecked")
			@Override
			public List<ParkOrgan> doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createQuery("FROM ParkOrgan po WHERE po.deleted = 0 ORDER BY po.sort");
				query.setFirstResult(0);
				query.setMaxResults(count);
				List<ParkOrgan> poList = query.list();
				if (poList != null && poList.size() > 0) {
					List<Integer> poCodes = new ArrayList<Integer>();
					for (ParkOrgan po : poList) {
						poCodes.add(po.getCode());
					}
					StringBuffer hqlBuffer = new StringBuffer("FROM OrganSynopsisAnnex osa WHERE osa.type = 1 AND osa.odc IN (");
					for (Integer code : poCodes) {
						hqlBuffer.append(code + ",");
					}
					hqlBuffer.deleteCharAt(hqlBuffer.lastIndexOf(",")).append(")");
					query = session.createQuery(hqlBuffer.toString());
					List<OrganSynopsisAnnex> osaList = query.list();
					if (osaList != null && osaList.size() > 0) {
						for (ParkOrgan po : poList) {
							Integer poCode = po.getCode();
							for (OrganSynopsisAnnex osa : osaList) {
								Integer osaCode = osa.getOdc();
								if (poCode.equals(osaCode)) {
									po.addList(osa);
									po.setLogo("organSynopsisAnnex/" + osa.getSn());
								}
							}
						}
					}
					return poList;
				}
				return null;
			}
		}); 
	}

}
